package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcChats;
import com.cloud.bc.model.entity.BcFriends;
import com.cloud.bc.model.entity.BcGifts;
import com.cloud.bc.model.entity.BcNotices;
import com.cloud.bc.model.enums.CodeEnum;
import com.cloud.bc.model.request.ChatRequest;
import com.cloud.bc.model.request.LikeRequest;
import com.cloud.bc.model.request.PhotoReq;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.model.response.RecentChatResponse;
import com.cloud.bc.service.BcChatsService;
import com.cloud.bc.service.BcFriendsService;
import com.cloud.bc.service.BcUsersService;
import lombok.AllArgsConstructor;
import org.aspectj.weaver.bcel.BcelFieldRef;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/chat")
public class Ajax_ChatController {

    private final BcChatsService bcChatsService;
    private final BcUsersService bcUsersService;
    private final BcFriendsService bcFriendsService;

    /**
     * 最近联系
     */
    @GetMapping("")
    public AjaxResp index(HttpServletRequest request, HttpServletResponse response,
                          LikeRequest req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        Page page = new Page();
        page.setCurrent(Convert.toInt(req.getPage(), 1));
        page.setSize(10);
        AjaxResp ajaxResp = new AjaxResp();
        List<RecentChatResponse> list = bcChatsService.selectRecentChat(page, userInfo.getId());
        ajaxResp.setList(list);
        return ajaxResp;
    }

    @PostMapping("send")
    public AjaxResp send(HttpServletRequest request, HttpServletResponse response,
                         ChatRequest req) {
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        AjaxResp ajaxResp = new AjaxResp();
        BcFriends bcFriends = new BcFriends();
        bcFriends.setUserId(userInfo.getId());
        bcFriends.setTargetId(req.getTargetId());
        int count = bcFriendsService.existsFriend(userInfo.getId(), req.getUserId());
        if (count <= 0) {
            //还不是好友
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("与对方还不是好友");
            ajaxResp.setCode(CodeEnum.NOFRIEND.getCode());
            return ajaxResp;
        }
        BcChats bcChats = new BcChats();
        bcChats.setUserId(userInfo.getId());
        bcChats.setTargetId(req.getTargetId());
        bcChats.setWords(req.getWords());
        bcChats.setUidpid(userInfo.getId() + "_" + req.getTargetId() + "_");
        bcChats.setWordType(req.getWordType());
        bcChatsService.save(bcChats);
        bcChats.setCreateTime(new Date());
        ajaxResp.setObj(bcChats);
        return ajaxResp;
    }

    @GetMapping("get")
    public AjaxResp get(HttpServletRequest request, HttpServletResponse response,
                        ChatRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        return ajaxResp;
    }

    @GetMapping("getall")
    public AjaxResp getall(HttpServletRequest request, HttpServletResponse response,
                           ChatRequest req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        req.setUserId(userInfo.getId());
        List<BcNotices> list = bcChatsService.selectChatList(req);
        ajaxResp.setList(list);
        return ajaxResp;
    }

    @PostMapping("delete")
    public AjaxResp delete(HttpServletRequest request, HttpServletResponse response,
                           BcChats req) {
        AjaxResp ajaxResp = new AjaxResp();
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        req.setUserId(userInfo.getId());
        req.setTargetId(req.getTargetId());
        bcChatsService.delete(req);
        return ajaxResp;
    }
}

